﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using libConn;

namespace ApartmentSale
{
	public partial class DiaCode : Window
	{
		public DiaCode()
		{
			this.InitializeComponent();
		}
        
        private const int RULELENGTH = 8;

        private int[] bitRule = new int[RULELENGTH];

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btOK_Click(object sender, RoutedEventArgs e)
        {
            boxData();

            int score = 0;
            for (int i = 0; i < RULELENGTH; i+=2 )
            {
                if (bitRule[i] != 0 && bitRule[i + 1] != 0)
                    score++;
            }

            if (score == 0)
            {
                MessageBox.Show("The current rule meaningless.");
                return;
            }

            bool repeat = false;
            for (int i = 0; i < RULELENGTH - 2; i += 2)
            {
                if (bitRule[i] == 0)
                    continue;

                for (int j = i + 2; j < RULELENGTH; j += 2)
                {
                    if (bitRule[i] == bitRule[j])
                    {
                        repeat = true;
                        break;
                    }

                    if (repeat)
                        break;
                }
            }

            if (repeat)
            {
                MessageBox.Show("Duplicate the current rules of the field.");
                return;
            }

            if (setData())
                this.Close();
        }

        private void diaCode_Loaded(object sender, RoutedEventArgs e)
        {
            if (getData())
            {
                int i = 0;
                cbBit1.SelectedIndex = bitRule[i++];
                tbBit1.Text = bitRule[i++].ToString();

                cbBit2.SelectedIndex = bitRule[i++];
                tbBit2.Text = bitRule[i++].ToString();

                cbBit3.SelectedIndex = bitRule[i++];
                tbBit3.Text = bitRule[i++].ToString();

                cbBit4.SelectedIndex = bitRule[i++];
                tbBit4.Text = bitRule[i++].ToString();
            }
            else
            {
                this.Close();
            }
        }

        private void boxData()
        {
            int i = 0;
            bitRule[i++] = cbBit1.SelectedIndex;
            bitRule[i++] = Convert.ToInt32(tbBit1.Text);
            bitRule[i++] = cbBit2.SelectedIndex;
            bitRule[i++] = Convert.ToInt32(tbBit2.Text);
            bitRule[i++] = cbBit3.SelectedIndex;
            bitRule[i++] = Convert.ToInt32(tbBit3.Text);
            bitRule[i++] = cbBit4.SelectedIndex;
            bitRule[i++] = Convert.ToInt32(tbBit4.Text);
        }

        private bool getData()
        {
            string errMessage = null;
            string str = "Select * From apartment_code WHERE id = 1";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Failed to read the apartment code naming",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            if (oleDT.Rows.Count < 1)
            {
                System.Windows.MessageBox.Show(
                    "Naming the apartment code is not set the initial value.",
                    "Failed to read the apartment code naming",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            DataRow r = oleDT.Rows[0];
            
            for (int i = 0; i < RULELENGTH; i++)
            {
                bitRule[i] = Convert.ToInt32(r[i + 1]);
            }

            return true;
        }

        private bool setData()
        {
            int i = 0;
            string errMessage = null;
            string str = "UPDATE apartment_code SET bittype1 = " + bitRule[i++].ToString() + " , ";
            str += "bitlength1 = " + bitRule[i++].ToString() + " , ";
            str += "bittype2 = " + bitRule[i++].ToString() + " , ";
            str += "bitlength2 = " + bitRule[i++].ToString() + " , ";
            str += "bittype3 = " + bitRule[i++].ToString() + " , ";
            str += "bitlength3 = " + bitRule[i++].ToString() + " , ";
            str += "bittype4 = " + bitRule[i++].ToString() + " , ";
            str += "bitlength4 = " + bitRule[i++].ToString();
            str += " WHERE id = 1";

            try
            {
                libConn.AccdbConn.exec(str, ref errMessage);
            }
            catch (Exception e)
            {
                errMessage = e.Message;
            }

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Save apartment code naming failure",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
            }

            return true;
        }

        private void tbBit_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int numberVal;

            if (int.TryParse(tb.Text, out numberVal))
            {
                if (numberVal > 0 && numberVal < 5)
                {
                    return;
                }
            }

            tb.Text = "0";
        }
        
	}
}